package ru.spb.osll.myfit.server;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import static ru.spb.osll.myfit.server.parser.PageParserUtils.getFullPath;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map.Entry;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import junit.framework.Assert;

import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import ru.spb.osll.myfit.common.TextParsingTools;
import ru.spb.osll.myfit.server.Configurator;

public class ConfiguratorTest {

	private static final String CONFIG_FILE = new String(
			"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<doc>\n"
					+ " <jars name=\"JARS\" path=\"lib\"></jars>\n"
					+ " <fixtures name=\"FIXTURES\" path=\"lib"
					+ File.separator + "fixtures\"></fixtures>\n"
					+ " <root name=\"ROOT\" path=\"tst" + File.separator
					+ "testData\"></root>\n"
					+ "<execresults name=\"EXECRESULTS\" path=\"testExecuteResults\"></execresults>\n"
					+ "<resources name=\"RESOURCES\" path=\"etc\"></resources>\n"
					+ "</doc>\n");

	private static final Configurator m_configurator = getConfigurator();

	private static Configurator getConfigurator() {
		try {
			DocumentBuilder xml = null;
			xml = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			InputStream is = new ByteArrayInputStream(CONFIG_FILE.getBytes());
			Document doc = xml.parse(is);
			Configurator configurator = Configurator.getInstance();
			configurator.loadConfigFile(doc);
			return configurator;
		} catch (ParserConfigurationException ex) {
		} catch (SAXException ex) {
		} catch (IOException ex) {
		}

		return null;
	}

	@Test
	public void testConfigurator() {
		Assert.assertEquals("lib", m_configurator.getJars());
		Assert.assertEquals("lib" + File.separator + "fixtures", m_configurator
				.getFixtures());
		Assert.assertEquals("tst" + File.separator + "testData", m_configurator
				.getRoot());
		Assert.assertEquals("testExecuteResults", m_configurator.getExecResults());
		Assert.assertEquals("etc", m_configurator.getResources());
	}

	@Test
	public void testGetFullPath() {
		HashMap<String, String> paths = new HashMap<String, String>();

		paths.put("", "tst" + File.separator + "testData" + File.separator
				+ "content.txt");
		paths.put("testPath", "tst" + File.separator + "testData"
				+ File.separator + "testPath" + File.separator + "content.txt");
		paths.put("ancestor.path.testPath", "tst" + File.separator + "testData"
				+ File.separator + "ancestor" + File.separator + "path"
				+ File.separator + "testPath" + File.separator + "content.txt");
		paths.put("12!.11&", "tst" + File.separator + "testData"
				+ File.separator + "12!" + File.separator + "11&"
				+ File.separator + "content.txt");
		paths.put(null, "tst" + File.separator + "testData" + File.separator
				+ "content.txt");

		for (Entry<String, String> entry : paths.entrySet()) {
			String fullPath = getFullPath(m_configurator.getRoot(), entry
					.getKey());
			assertEquals(entry.getValue(), fullPath);
		}
	}

	@Test
	public void testGetPathDeclaration() {
		String pathDeclaration = m_configurator.getPathDeclaration();

		pathDeclaration = pathDeclaration.substring(TextParsingTools.PATH
				.length());
		String[] jars = pathDeclaration.split(";");

		for (String jar : jars) {
			if (jar.equals(TextParsingTools.CR_and_NEL))
				return;

			File f = new File(jar);

			assertTrue(f.exists());
		}
		assertTrue(jars.length > 5);
	}
}
